package com.example.wb.myfirstandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CalculatorAction extends BaseActivity implements View.OnClickListener {

    Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
    Button btnBacksapce, btnCE, btnC, btnAdd, btnSub, btnMul, btnDiv, btnEqu, btnPoint;
    TextView tvResult;

    double num1 = 0, num2 = 0;
    double result = 0;
    String op = ""; //判断操作数
    boolean isClickEqu = false; //判断是否按来"="按钮

    @Override
    protected int getLayoutId() {
        return R.layout.activity_calculator_action;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_calculator_action);
        btn0 = (Button) findViewById(R.id.btn0);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        btn8 = (Button) findViewById(R.id.btn8);
        btn9 = (Button) findViewById(R.id.btn9);
        btnBacksapce = (Button) findViewById(R.id.btnBackspace);
        btnCE = (Button) findViewById(R.id.btnCE);
        btnC = (Button) findViewById(R.id.btnC);
        btnEqu = (Button) findViewById(R.id.btnEQU);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnSub = (Button) findViewById(R.id.btnSub);
        btnMul = (Button) findViewById(R.id.btnMul);
        btnDiv = (Button) findViewById(R.id.btnDiv);
        btnPoint = (Button) findViewById(R.id.btnPoint);
        tvResult = (TextView) findViewById(R.id.tvResult);

        btn0.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btnBacksapce.setOnClickListener(this);
        btnCE.setOnClickListener(this);
        btnC.setOnClickListener(this);
        btnEqu.setOnClickListener(this);
        btnAdd.setOnClickListener(this);
        btnSub.setOnClickListener(this);
        btnMul.setOnClickListener(this);
        btnDiv.setOnClickListener(this);
        btnPoint.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Button thisBtn = (Button) v;
        String myStr = tvResult.getText().toString();
        String btnTxt = thisBtn.getText().toString();
        switch (v.getId()) {
            case R.id.btnBackspace:
                try {
                    if (myStr.length() == 1) {
                        tvResult.setText("0");
                    } else
                        tvResult.setText(myStr.substring(0, myStr.length() - 1));
                } catch (Exception e) {
                    tvResult.setText("0");
                }
                break;
            case R.id.btnCE:
                tvResult.setText("0");
                break;
            case R.id.btnPoint:
                if (myStr.indexOf(".") >= 0) {
                    return;
                } else {
                    myStr += ".";
                    tvResult.setText(myStr);
                }
                break;
            case R.id.btn0:
            case R.id.btn1:
            case R.id.btn2:
            case R.id.btn3:
            case R.id.btn4:
            case R.id.btn5:
            case R.id.btn6:
            case R.id.btn7:
            case R.id.btn8:
            case R.id.btn9:
                if (isClickEqu) {
                    tvResult.setText("0");
                    isClickEqu = false;
                    myStr = "0";
                }
                if (!myStr.equals("0")) {
                    tvResult.setText(myStr + btnTxt);
                } else
                    tvResult.setText(btnTxt);
                break;
            case R.id.btnAdd:
            case R.id.btnSub:
            case R.id.btnMul:
            case R.id.btnDiv:
                if (myStr.equals("0")) {
                    return;
                }
                num1 = Double.valueOf(myStr);
                tvResult.setText("0");
                isClickEqu = false;
                op = btnTxt;
                break;
            case R.id.btnEQU:
                if (myStr.equals("0"))
                    return;
                num2 = Double.valueOf(myStr);
                switch (op) {
                    case "":
                        result = num2;
                        break;
                    case "+":
                        result = num1 + num2;
                        break;
                    case "-":
                        result = num1 - num2;
                        break;
                    case "*":
                        result = num1 * num2;
                        break;
                    case "/":
                        if (num2 != 0) {
                            result = num1 / num2;
                        }
                        break;
                    default:
                        result = 0;
                        break;
                }
                tvResult.setText(String.valueOf(result));
                isClickEqu = true;
                break;
            case R.id.btnC:
                op="";
                num1=0;
                num2=0;
                tvResult.setText("0");
                break;
            default:
                break;
        }
    }
}
